import { HttpError, useTransaction } from "@sker/core"
import { useReqBody } from "@sker/runtime"
import { z } from "zod"

const schema = z.object({
    field: z.string()
})
export default async () => {
    const body = schema.parse(useReqBody())
    return await useTransaction(async (manager) => {
        const res = await manager.query(`select nextval('${body.field}_seq'::regclass) as nextval`)
        if (res.length > 0) {
            return res[0].nextval
        } else {
            throw new HttpError(`获取${body.field}失败`)
        }
    })
}